import inputs.Color;
import inputs.Input;

import java.util.ArrayList;

import oldmodifiers.Modifier;
import oldmodifiers.Modifiers;
import oldmodifiers.Sum;

import outputAdapters.OutputRanges;
import outputAdapters.ScaleAdapter;

import midi.IntervalOperations;
import midi.MidiController;
import midi.MidiPhrase;
import midi.IntervalOperations.interval;
import midi.patterns.MyPatterns;


import processing.core.*;
import scales.MidiPhraser;
import codeanticode.gsvideo.*;

/**
 * Processing Applet which demonstrates grabbing colors from a webcam
 * and passing them to midi output.
 * 
 * @author Dylan James
 *
 */
public class ModifierTest extends PApplet{
	GSCapture cam; // Capture object
	int vidX = 640, vidY = 480; // Size of the applet window and capture
	int redVal, greenVal, blueVal; // Variables to hold the value of each color
	int colorVal, totalVal, adaptedVal; // Hold the total color value
	PFont font; // Font to write on the screen with
	Color redColor;
	Color blueColor;
	Modifier rbMod;
	ScaleAdapter outputAdapter;
	
	public void setup() {
		// Initialize the applet window
		size(vidX, vidY);
		background(0);
		fill(255);
		stroke(255);
		
		// Load and set the font
		font = loadFont("CourierNew36.vlw");
		textFont(font, 32);
		
		frameRate(30);
		
		// Initialize the capture device
		cam = new GSCapture(this, vidX, vidY);
		
		redColor = new Color(0);
		blueColor = new Color(0);
		ArrayList<Input> rbList = new ArrayList<Input>(2);
		rbList.add(redColor);
		rbList.add(blueColor);
		rbMod = new Modifier(rbList, Modifiers.Sum);
		rbMod.setRun(false);
		
		outputAdapter = new ScaleAdapter(OutputRanges.Pitch, rbMod);
	}

	public void draw(){
		// Grab image from the webcam if possible
		if (cam.available() == true){
			cam.read();
		}
		// Store it
		image(cam, 0, 0);
		// Get the color from the current mouse position
		colorVal = get(mouseX, mouseY);
		
		// Store each color individual
		redVal = (int) red(colorVal);
		greenVal = (int) green(colorVal);
		blueVal = (int) blue(colorVal);
		totalVal = (redVal + greenVal + blueVal) / 3;		
		
		redColor.setValue(redVal);
		blueColor.setValue(blueVal);
		rbMod.UpdateInput();
		
		outputAdapter.UpdateInput();
		
		totalVal = rbMod.getValue();
		
		adaptedVal = outputAdapter.getValue();
		
		fill(0, 0);
		// Draw a circle by the cursor
		ellipse(mouseX, mouseY, 10, 10);
		
		
		fill (255, 255);
		
		// Draw each color to the applet
		text("R = " + redVal, 5, 30);
		text("G = " + greenVal, 5, 60);
		text("B = " + blueVal, 5, 90);
		text("T = " + totalVal, 5, 120);
		text("A = " + adaptedVal, 5, 150);
	}
	
	
	// Allows the program to be run as a java application.
	public static void main(String args[]){
		PApplet.main(new String[] { "--present", "WebCamColorMidi"});
	}
}
